#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using System.Web;
using Agresso.Interface.Layer.Service;
using System.Collections;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class WinSDSItemCodeAccountMappingForm : Form
    {
        i_winsds_itemcode_account_mapping m_grade;
        UserControlWinSDSItemCodeAccountMapping m_parent;
        public WinSDSItemCodeAccountMappingForm(i_winsds_itemcode_account_mapping grade, UserControlWinSDSItemCodeAccountMapping parent)
        {
            InitializeComponent();
            this.Text = "WinSDS Product Account Mapping";
            m_grade = grade;
            m_parent = parent;

            textBoxGrade.Enabled = grade.itemCode.ToLower() != "default";
            textBoxGrade.ReadOnly = grade.itemCode.ToLower() == "default";
            textBoxAgrAccountGRN.Enabled = grade.itemCode.ToLower() != "default";
            textBoxAgrAccountGRN.ReadOnly = grade.itemCode.ToLower() == "default";

            textBoxGrade.Text = grade.itemCode;
            textBoxAgrAccount.Text = grade.account;
            textBoxAgrAccountGRN.Text = grade.dim_2;
        }
        public WinSDSItemCodeAccountMappingForm(string grade)
        {
            InitializeComponent();
            this.Text = "WinSDS Product Account Mapping";
            WinSDSService service = new WinSDSService();
            m_grade = service.FindByItemCOdeWinSDSAccountMapping(grade);

            textBoxGrade.Text = m_grade.itemCode;
            textBoxAgrAccount.Text = m_grade.account;
            textBoxAgrAccountGRN.Text = m_grade.dim_2;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxGrade.Text == "")
                {
                    MessageBox.Show("Please type the Item Code");
                    return;
                }
                UpdateSubject();
                this.Close();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void UpdateSubject()
        {
           
            m_grade.itemCode = textBoxGrade.Text;
            m_grade.account = textBoxAgrAccount.Text;
            m_grade.dim_2 = textBoxAgrAccountGRN.Text;
            WinSDSService service = new WinSDSService();
            if (m_grade.id == 0)
            {
                service.InsertWinSDSAccountMapping(m_grade);
                if (m_parent != null)
                    m_parent.RefreshItems();
            }
            else
            {
                service.UpdateWinSDSAccountMapping(m_grade);
                if (m_parent != null)
                    m_parent.UpdateSelectedItem(m_grade);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void textBoxAgrDescription_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            buttonApply.Enabled = true;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxGrade.Text == "")
                {
                    MessageBox.Show("Please type the Item Code");
                    return;
                }
                UpdateSubject();
                if (m_grade.id == 0)
                    this.Close();
                else
                    buttonApply.Enabled = false;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void SoftmarVesselMappingForm_Load(object sender, EventArgs e)
        {
            textBoxGrade.Focus();
            textBoxGrade.SelectAll();
        }
    }
}